package com.yuanrui.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.yuanrui.subject.application.convert.SubjectAnswerDTOConverter;
import com.yuanrui.subject.application.convert.SubjectInfoDTOConverter;
import com.yuanrui.subject.application.dto.SubjectInfoDTO;
import com.yuanrui.subject.common.entity.PageResult;
import com.yuanrui.subject.common.entity.Result;
import com.yuanrui.subject.domain.entity.SubjectAnswerBO;
import com.yuanrui.subject.domain.entity.SubjectInfoBO;
import com.yuanrui.subject.domain.service.SubjectInfoDomainService;
import com.yuanrui.subject.infra.basic.entity.SubjectInfoEs;
import com.yuanrui.subject.infra.basic.service.SubjectEsService;
import lombok.extern.slf4j.Slf4j;

import org.apache.ibatis.annotations.Param;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 刷题controller
 *
 * @author: ChickenWing
 * @date: 2023/10/1
 */
@RestController
@Slf4j
@RequestMapping("/subject")
public class SubjectController {

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    @Resource
    private SubjectEsService subjectEsService;

    @Resource
    private RocketMQTemplate rocketMQTemplate;
    /**
     * 添加题目
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.add.dto:{}", JSON.toJSONString(subjectDTO));
            }
            Preconditions.checkNotNull(subjectDTO.getSubjectName(), "题目名称不能为空");
            Preconditions.checkNotNull(subjectDTO.getSubjectDifficult(), "难度不能为空");
            Preconditions.checkNotNull(subjectDTO.getSubjectType(), "题目类型不能为空");
            Preconditions.checkNotNull(subjectDTO.getSubjectScore(), "分数不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectDTO.getCategoryIds()), "题目分类不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectDTO.getLabelIds()), "题目标签不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertToBo(subjectDTO);
            List<SubjectAnswerBO> subjectAnswerBOS = SubjectAnswerDTOConverter.INSTANCE.convertToBo(subjectDTO.getOptionList());
            subjectInfoBO.setOptionList(subjectAnswerBOS);
            subjectInfoDomainService.add(subjectInfoBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增题目失败");
        }
    }

    /**
     * 查询题目列表
     */
    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> list(@RequestBody SubjectInfoDTO subjectDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.list.dto:{}", JSON.toJSONString(subjectDTO));
            }
            Preconditions.checkNotNull(subjectDTO.getCategoryId(), "分类id不能为空");
            Preconditions.checkNotNull(subjectDTO.getLabelId(), "标签id不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertToBo(subjectDTO);
            PageResult<SubjectInfoBO> subjectPage = subjectInfoDomainService.getSubjectPage(subjectInfoBO);
            return Result.ok(subjectPage);
        } catch (Exception e) {
            log.error("SubjectController.list.error:{}", e.getMessage(), e);
            return Result.fail("查询题目列表失败");
        }
    }

    /**
     * 查询题目详情信息
     */
    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> getSubjectDetail(@RequestBody SubjectInfoDTO subjectDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectDetail.dto:{}", JSON.toJSONString(subjectDTO));
            }
            Preconditions.checkNotNull(subjectDTO.getId(), "题目id不能为空");
            SubjectInfoBO subjectInfoBO1 = SubjectInfoDTOConverter.INSTANCE.convertToBo(subjectDTO);
            SubjectInfoBO subjectInfoBO = subjectInfoDomainService.querySubjectInfo(subjectInfoBO1);
            SubjectInfoDTO subjectInfoDTO = SubjectInfoDTOConverter.INSTANCE.convertBoDto(subjectInfoBO);
            return Result.ok(subjectInfoDTO);
        } catch (Exception e) {
            log.error("SubjectController.getSubjectDetail.error:{}", e.getMessage(), e);
            return Result.fail("查询题目详情失败");
        }
    }

    /**
     * 全文检索
     */
    @PostMapping("/getSubjectPageBySearch")
    public Result<PageResult<SubjectInfoEs>> getSubjectPageBySearch(@RequestBody SubjectInfoDTO subjectDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPageBySearch.dto:{}", JSON.toJSONString(subjectDTO));
            }
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertToBo(subjectDTO);
            PageResult<SubjectInfoEs> subjectPage = subjectInfoDomainService.getSubjectPageBySearch(subjectInfoBO);
            return Result.ok(subjectPage);
        } catch (Exception e) {
            log.error("SubjectController.getSubjectPageBySearch.error:{}", e.getMessage(), e);
            return Result.fail("全文检索失败");
        }
    }

    /**
     * 获取当前榜单
     */
    @PostMapping("/getContributeList")
    public Result<List<SubjectInfoDTO>> getContributeList(@RequestBody SubjectInfoDTO subjectDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getContributeList.dto:{}", JSON.toJSONString(subjectDTO));
            }
            List<SubjectInfoBO> subjectInfoBOS = subjectInfoDomainService.getContributeList();
            List<SubjectInfoDTO> dtoList = SubjectInfoDTOConverter.INSTANCE.toDTOList(subjectInfoBOS);
            return Result.ok(dtoList);
        } catch (Exception e) {
            log.error("SubjectController.getContributeList.error:{}", e.getMessage(), e);
            return Result.fail("获取当前榜单失败");
        }
    }

    /**
     * 测试mq
     */
    @PostMapping("/pushMessage")
    public Result<Boolean> pushMessage(@Param("id") Long id) {
        rocketMQTemplate.convertAndSend("first-topic", "subject-test"+id);
        return Result.ok(true);
    }
}
